package com.roncoo.education.app.gateway;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.server.WebFilter;

/**
 * 解决跨域问题
 */
@Configuration
public class CorsGlobalConfiguration {
 
    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE)
    public WebFilter corsFilter() {
        return (serverWebExchange, webFilterChain) -> {        	
        	String path = serverWebExchange.getRequest().getPath().toString();
        	if (path.startsWith("/oauth")) {        		
        		serverWebExchange.getResponse().getHeaders().set("Access-Control-Allow-Origin", "*");
        		serverWebExchange.getResponse().getHeaders().set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
        		serverWebExchange.getResponse().getHeaders().set("Access-Control-Allow-Headers", "Content-Type, Accept, Authorization, X-Requested-With, remember-me, Origin");
        	}
        	
            return webFilterChain.filter(serverWebExchange);
        };
    }
}
